package com.example.redis.demo.configuration;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.protocol.RedisCommand;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;

import org.springframework.cache.annotation.CachingConfigurerSupport;

import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @description:todo
 * @auth yangzhiwei
 * @date 2021-06-30 16:25
 */


public class RedisCacheConfig extends CachingConfigurerSupport {

    /**
     * Jedis使用
     */
    public static JedisPool getJedisPool() {
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        JedisPool jedisPool = new JedisPool( genericObjectPoolConfig,"localhost", 6378, 10, "yang3");
        return jedisPool;
    }
    public static  void jedisTest(JedisPool jedisPool) {
//        JedisPoolConfig poolConfig = new JedisPoolConfig();
//        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        Jedis jedis = jedisPool.getResource();
        jedis.set("YZW","yang1");
        System.out.println(jedis.get("YZW"));
        //jedis.get();
    }

    public static void main(String[] args) {
       // jedisTest(getJedisPool());
        getConnection();
    }

    /**
     *Lettuce 使用
     */
    public static StatefulRedisConnection getConnection() {
        RedisURI redisURI = RedisURI.builder().withHost("localhost")
                .withPort(6376)
                //.withAuthentication("redis","yang1")
                .withPassword("yang1")
                .build();

        RedisClient redisClient = RedisClient.create(redisURI);
        StatefulRedisConnection<String,String> connection = redisClient.connect();
        RedisCommands<String,String> redisCommand = connection.sync();
        String value = redisCommand.get("YZW");
        System.out.println(value);
        return connection;
    }





    public RedisTemplate<String,Object> objectRedisTemplate(RedisConnectionFactory connectionFactory) {
        RedisAutoConfiguration re;
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxWaitMillis(10000);
        return jedisPoolConfig;
    }
    //@Bean
    public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
//        redisStandaloneConfiguration.setHostName(host);
//        redisStandaloneConfiguration.setDatabase(database);
//        redisStandaloneConfiguration.setPassword(pwd);
//        redisStandaloneConfiguration.setPort(port);
        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
//        jpcb.poolConfig(jedisPoolConfig);
        JedisClientConfiguration jedisClientConfiguration = jpcb.build();
        return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
    }



//    public void jedisTest(RedisConnectionFactory factory) {
//        //RedisTemplate redisTemplate
//        RedisConnection redisConnection = factory.getConnection();
//        redisConnection.getSubscription()
//        Jedis jedis = new Jedis();
//
//        RedisProperties.Jedis jedis;
//
//    }
}
